const app = getApp()
// import { Toast } from 'tdesign-miniprogram';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mobile_phone: '',
    id: '',
    receiving_address: {
      name: '',
      phone_number: '',
      address: ''
    },
    keys: {
      label: 'name',
      value: 'id',
      children: 'subs',
    },
    areaOptions: [],
    areaVisible: false,

  },
  onColumnChange(e) {
    console.log('picker pick:', e);
  },

  onPickerChange(e) {
    const {
      key
    } = e.currentTarget.dataset;
    const {
      value
    } = e.detail;
    console.log(e.detail);
    console.log('picker change:', e.detail);
    this.setData({
      [`${key}Visible`]: false,
      [`${key}Value`]: value,
      [`${key}Text`]: value.join(' '),
    });
  },

  onPickerCancel(e) {
    const {
      key
    } = e.currentTarget.dataset;
    console.log(e, '取消');
    console.log('picker1 cancel:');
    this.setData({
      [`${key}Visible`]: false,
    });
  },
  getUserInfo(mobile_phone) {
    let data = {
      token: wx.getStorageSync('token'),
      mobile_phone: mobile_phone
    }
    let _this = this
    app.request('get', '/user/getUserInfo', data, res => {
      let userInfo = res.data.data
      let obj = userInfo.receiving_address.filter(res => {
        return res.id == _this.data.id
      })
      _this.setData({
        'receiving_address': obj[0]
      })
      // console.log(obj);
    })
  },
  getAreaInfo() {
    let _this = this
    app.request('get', '/common/getAreaInfo', {}, res => {
      console.log(res);
      _this.setData({
        areaOptions: res.data.data.areaList
      })
      // areaOptions
    })
  },
  areaChange(e) {
    const {
      selectedOptions
    } = e.detail;
    let obj = JSON.parse(JSON.stringify(this.data.receiving_address))
    obj.province = selectedOptions[0].name
    obj.city = selectedOptions[1].name
    obj.area = selectedOptions[2].name
    this.setData({
      receiving_address: obj
    })

  },
  showArea() {
    this.setData({
      areaVisible: true
    })
  },
  changeName(e) {
    let fieldName = e.target.dataset.fieldName;
    let obj = JSON.parse(JSON.stringify(this.data.receiving_address))
    obj.name = e.detail.value
    this.setData({
      receiving_address: obj
    })
  },
  changePhone(e) {
    let fieldName = e.target.dataset.fieldName;
    let obj = JSON.parse(JSON.stringify(this.data.receiving_address))
    obj.phone_number = e.detail.value
    this.setData({
      receiving_address: obj
    })
  },

  change_detail_address(e) {
    let fieldName = e.target.dataset.fieldName;
    let obj = JSON.parse(JSON.stringify(this.data.receiving_address))
    obj.detail_address = e.detail.value
    this.setData({
      receiving_address: obj
    })
  },
  editAddress(e) {
    let obj = this.data.receiving_address
    let mobile_phone = this.data.mobile_phone


    if (obj.name == '') {
      wx.showToast({
        title: '请输入收货人',
        icon: 'error',
        duration: 1500
      })
      return
    }
    if (obj.phone_number == '') {
      wx.showToast({
        title: '请输入手机号',
        icon: 'error',
        duration: 1500
      })
      return
    }
    if (obj.province == '') {
      wx.showToast({
        title: '请选择地区',
        icon: 'error',
        duration: 1500
      })
      return
    }
    if (obj.detail_address == '') {
      wx.showToast({
        title: '请输入详细地址',
        icon: 'error',
        duration: 1500
      })
      return
    }

    let data = {
      receiving_address: obj
    }
    let _this = this
    app.request('post', `/user/editUserInfo?mobile_phone=${mobile_phone}`, data, res => {
      wx.showToast({
        title: '修改成功',
        icon: 'success',
        duration: 1500
      })
      let mobile_phone = _this.data.mobile_phone
      wx.redirectTo({
        url: `/pages/address/address?mobile_phone=${mobile_phone}`,
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      mobile_phone: options.mobile_phone,
      id: options.id
    })
    if (options.id) {
      this.getUserInfo(options.mobile_phone)
    } else {
      let obj = {
        phone_number: '',
        name: '',
        province: '',
        city: '',
        area: '',
        detail_address: '',
        is_default: false
      }
      this.setData({
        receiving_address: obj
      })
    }
    this.getAreaInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})